package com.shopping.servelet;

import java.io.IOException;
import java.io.Writer;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.shopping.entity.Goods;

@WebServlet("/DeleteServelet")
public class DeleteServelet extends HttpServlet{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		req.setCharacterEncoding("utf-8");
		resp.setCharacterEncoding("utf-8");
		resp.setContentType("Text/html");
		String id=req.getParameter("id");//得到ID
		String num=req.getParameter("num");//得到数量
		Writer out = resp.getWriter() ;
		System.out.println("ID="+id+","+"NUM="+num);
		HttpSession session=req.getSession(true);
		
		if(session.getAttribute("list1")==null){
		  out.write("购物车为空");
//			List<Goods> list=(List<Goods>) session.getAttribute("list1");
//			for (Goods goods : list) {
//				str+="{\"id\":\""+goods.getId()+"\",\"name\":\""+goods.getName()+"\",\"price\":"+goods.getPrice()+",\"num\":"+goods.getNum()+"},";
//			}
		}else{
			List<Goods> list=(List<Goods>) session.getAttribute("list1");
			int number = Integer.valueOf(num);
			int scid = Integer.valueOf(id);
			if(list.size()==0){
				
			}else{
				for (Goods goods : list) {
					System.out.println("++"+goods);
					if(goods.getId().equals(id)&&goods.getNum()==number){
						System.out.println(goods);
						list.remove(goods); 
						break;
					}	
				}  
			    }
				session.setAttribute("list1", list);
		}
	}
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doGet(req, resp);
	}
}
